/******************************************************************************/
/*  Class UART0                                                               */
/*  PO: S. Maslyakov, rusoil.9@gmail.com                                      */
/*                                                                            */
/*  Revision:     1.1                                                         */
/*  Date:         2010/10/25 11:59:33                                         */
/******************************************************************************/


#include "data_types.h"
#include "Core\core.h"
#include "Uart\uart0.h"



//==============================================================================
// App: Init uart
//==============================================================================
void UART0::Init_Uart(const uint32_t _baudrate, const uint8_t _key) {
    U0IER = 0UL;                  // int dis

    PCONP_bit.PCUART0 = 1;

    PINSEL0_bit.P0_2 = 1;         // TXD
    PINSEL0_bit.P0_3 = 1;         // RXD

    FIO0DIR_bit.P0_2 = 1;         // TXD
    FIO0DIR_bit.P0_3 = 0;         // RXD

    PINMODE0_bit.P0_2 = 2;        // TXD PULL_RES - dis
    PINMODE0_bit.P0_3 = 2;        // RXD

    U0LCR = 0x80 | _key;          // 8 bits, no Parity, 1 Stop bit
    U0FDR_bit.DIVADDVAL = 0;
    U0FDR_bit.MULVAL = 0;

    uint32_t Br;

    if(_baudrate == 115200) {
        // ! for FPCLK=28800000
        U0DLM = 0;
        U0DLL = 13;
        U0FDR_bit.DIVADDVAL = 1;
        U0FDR_bit.MULVAL = 5;
    }
    else {
        Br = (FPCLK / 16) / _baudrate;
        U0DLM = Br / 256;
        U0DLL = Br % 256;
    };

    U0LCR = _key;                  // DLAB = 0

    U0FCR = 0x41;                 // Enable and reset TX and RX FIFO, size 4 bytes
}
//==============================================================================
//==============================================================================